<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form 
			:rules="state.rules"
			ref="userDialogFormRef" 
			:model="state.ruleForm" 
			size="default" 
			status-icon
			label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="标题" prop="title">
							<el-input v-model="state.ruleForm.title" placeholder="请输入标题" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="内容">
                            <Editor 
                            v-model:get-html="state.ruleForm.editor.htmlVal" 
                            v-model:get-text="state.ruleForm.editor.textVal" 
                            :disable="state.ruleForm.editor.disable" />
                        </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="状态">
							<el-switch v-model="state.ruleForm.status" 
							inline-prompt active-text="启" inactive-text="禁">
							</el-switch>
						</el-form-item>
					</el-col>
                    <el-form-item label="排序">
							<el-input-number v-model="state.ruleForm.sort" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { onMounted, reactive, ref, defineAsyncComponent } from 'vue';
import { useRoleApi } from '/@/api/roles';
import { ElMessage } from 'element-plus';
import { useUserApi } from '/@/api/user';

const Editor = defineAsyncComponent(() => import('/@/components/editor/index.vue'));
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		title: '', // 账户名称
        content: '', // 内容
		sort: 0, // 手机号
		status: 1, // 用户状态
        editor: {
            htmlVal:'',
            textVal: '',
            disable: false,
        },
	},
	rules: {
		name: { required: true, message: '请输入账户名称', trigger: 'blur' },
		mobile: { required: true, message: '请输入手机号', trigger: 'blur' },
		role: { required: true, message: '请选择关联角色', trigger: 'change' },
		password: { required: true, message: '请输入账户密码', trigger: 'blur' },
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	roleOptions: [] as RoleOptionType[],
	type: 'add',
});

// 打开弹窗
const openDialog = (type: string, row: RowUserType) => {
	state.type = type;
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改用户';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增用户';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	userDialogFormRef.value.resetFields();
		// });
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
const formRulesValidate = (pageRef: RefType) => {
	return new Promise((resolve) => {
		pageRef.value.validate((valid: boolean) => {
			if (valid) resolve(valid);
		});
	});
};
// 提交
const onSubmit = () => {
	Promise.all([
		formRulesValidate(userDialogFormRef),
	]).then(async() => {
		//请求后端添加数据
		if(state.type==='edit'){
			await useUserApi().postUserEdit(state.ruleForm);
		}else{
			await useUserApi().postUserAdd(state.ruleForm);
		}
		closeDialog();
		emit('refresh');
		ElMessage.success('操作成功');
	});
	// if (state.dialog.type === 'add') { }
};
//初始化角色数据
const getRoleOptions = async () => {
	await useRoleApi().getRoleOptions().then((res: any) => {
		state.roleOptions = res.data;
	});
};
onMounted(() => {
	getRoleOptions();
});

// 暴露变量
defineExpose({
	openDialog,
});
</script>
